package org.example.utils;


import okhttp3.*;
import java.io.File;
import java.io.IOException;

public class ApiClient {
    private static final String URL = "YOUR-API-URL-CREATED-IN-MODELARTS";
    private static final String TOKEN = "YOUR-CLOUD-TOKEN";
    public static String commodityMapRecognition(String picture_path) {
        OkHttpClient client = new OkHttpClient();

        // 创建文件对象
        File file = new File(picture_path);

        // 构建请求体
        RequestBody requestBody = new MultipartBody.Builder()
                .setType(MultipartBody.FORM)
                .addFormDataPart("images", file.getName(),
                        RequestBody.create(MediaType.parse("image/jpeg"), file))
                .build();

        // 构建请求
        Request request = new Request.Builder()
                .url(URL)
                .post(requestBody)
                .header("x-auth-token", TOKEN)
                .build();
        
        String result = null;
        // 发送请求并处理响应
        try (Response response = client.newCall(request).execute()) {

            // 假设服务器返回的是JSON格式的数据
            result = response.body().string();
        } catch (IOException e) {
            e.printStackTrace();
            return null;
        } finally {
            return result;
        }
    }
}